package view.plugin
{
	//聊天：下面一排
	import event.GlobalEvent;
	
	import fl.controls.List;
	import fl.events.ListEvent;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	import flash3.framework.Singleton;
	import flash3.net.MsgPacket;
	import flash3.net.NetFacade;
	
	import global.Resource;
	
	import model.MeInfoModel;
	import model.UserInfoModel;
	
	import net.*;
	
	import util.RichTextArea;
	
	import vo.Message;
	import vo.RoomUser;
	import vo.UserChat;
	import vo.UserInfo;
	
	public class ChatPanel extends Sprite
	{
		private var chatGroup:MovieClip;
		private var facePanel:MovieClip;
		private var input:MovieClip;
		private var btnSend:SimpleButton;
		private var btnFace:SimpleButton;
		private var btnLanguage:SimpleButton;
		private var inputTextArea:RichTextArea;
		private var outputTextArea:RichTextArea;
		private var curFaceX:int = -1;
		private var curFaceY:int = -1;
		private var faceRect:Rectangle;
		private var normalList:List;
		
		public function ChatPanel()
		{
			super();
			chatGroup = Resource.i.getMovieClip("chatGroup", "ChatGroup");
			facePanel = chatGroup.facePanel;
//			btnSend = chatGroup.btnSend;
			btnFace = chatGroup.btnFace;
			input = chatGroup.input;
			btnLanguage = chatGroup.btnLanguage;
			facePanel.visible = false;
			facePanel.rect.mouseEnabled = false;
			facePanel.rect.visible = false;
			faceRect = new Rectangle(0, 0, facePanel.width, facePanel.height);
			addChild(chatGroup);

			var txtFormat:TextFormat = new TextFormat("Courier New", 12, 0x0, false, false, false);	
			inputTextArea = new RichTextArea(273,25);
			inputTextArea.x = 8;
			inputTextArea.y = 4;
			inputTextArea.configXML = ChatFace.faceXML;
			inputTextArea.maxChar = 32;
			inputTextArea.textField.multiline = false;
			inputTextArea.textField.type = TextFieldType.INPUT;
			inputTextArea.textField.defaultTextFormat = txtFormat;
			inputTextArea.textField.addEventListener(KeyboardEvent.KEY_DOWN, onTextInputHandler);
			input.addChild(inputTextArea);
			input.visible  = false;
			chatGroup.addChild(facePanel);
			
			normalList = new List();
			normalList.width = 220;
			normalList.height = 100;
			normalList.x = btnLanguage.x - normalList.width + 20;
			normalList.y = btnLanguage.y - normalList.height - 5;
			var tf:TextFormat = new TextFormat();
			tf.size = 12;
			tf.leading = 5;
			tf.font = "verdana";
			normalList.setStyle("textFormat",tf);
			normalList.addItem({label:"快点啊，我等的花都谢了!"});
			normalList.addItem({label:"我对你的敬仰如滔滔江水，连绵不绝！"});
			normalList.addItem({label:"下次别让我遇到你！"});
			normalList.addItem({label:"我和你拼了！"});
			normalList.addItem({label:"维护宇宙和平就靠你了！"});
			normalList.addItem({label:"行行好，让我出一张吧T_T"});
			normalList.addItem({label:"不怕神一样的对手，就怕……"});
			normalList.addItem({label:"无尽的等待，让人蛋疼！"});
			normalList.addItem({label:"不要吵，认真打牌！"});
			normalList.addEventListener(ListEvent.ITEM_CLICK, onClickNormalList);
			normalList.visible = false;
			chatGroup.addChild(normalList);
			
			btnFace.addEventListener(MouseEvent.CLICK, onClickBtnFace);
			facePanel.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveFacePanel);
			facePanel.addEventListener(MouseEvent.CLICK, onClickFacePanel);
			facePanel.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverFacePanel);
			btnLanguage.addEventListener(MouseEvent.CLICK, onClickBtnLanguage);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage);
			
			this.x = -85; this.y = 527;
		}
		private function onAddToStage(evt:Event):void
		{
			stage.addEventListener(MouseEvent.CLICK, onMosueClickStage);
		}
		private function onRemoveFromStage(evt:Event):void
		{
			stage.removeEventListener(MouseEvent.CLICK, onMosueClickStage);
		}
		private function onMosueClickStage(evt:MouseEvent):void
		{
			facePanel.visible = false;	
			normalList.visible = false;
		}
		private function onTextInputHandler(evt:KeyboardEvent):void
		{
			if(evt.keyCode == 13)
			{
				sendMessage();
				inputTextArea.richText = "";
			}
		}
		private function onClickNormalList(evt:ListEvent):void
		{
			sendMessageExt(evt.item.label);
		}
		
		private function onClickBtnSend(evt:MouseEvent):void
		{
			sendMessage();
		}
		
		private function sendMessage():void
		{
			if(inputTextArea.richText == "")return;
			var userChat:UserChat = new UserChat();
			userChat.dwSendUserID = MeInfoModel.i.userInfo.dwUserID;
			userChat.dwTargetUserID = 0;
			userChat.crFontColor = 0x0;
			userChat.wChatLength = inputTextArea.cacheTextField.text.length;
			userChat.szChatMessage = inputTextArea.cacheTextField.text;
			NetFacade.i.send(UserGroup.MDM_GR_USER,UserGroup.SUB_GR_USER_CHAT,userChat);
		}
		private function sendMessageExt(message:String):void
		{
			var userChat:UserChat = new UserChat();
			userChat.dwSendUserID = MeInfoModel.i.userInfo.dwUserID;
			userChat.dwTargetUserID = 0;
			userChat.crFontColor = 0x0;
			userChat.wChatLength = message.length;
			userChat.szChatMessage = message;
			NetFacade.i.send(UserGroup.MDM_GR_USER,UserGroup.SUB_GR_USER_CHAT,userChat);
		}
		private function onClickBtnLanguage(evt:MouseEvent):void
		{
			normalList.selectedIndex = -1;
			normalList.visible = !normalList.visible;
			facePanel.visible = false;
			evt.stopImmediatePropagation();
		}
		
		private function onClickBtnFace(evt:MouseEvent):void
		{
			facePanel.visible = !facePanel.visible;
			if(facePanel.visible)
			{
				facePanel.select = false;
				facePanel.rect.visible = false;
			}
			normalList.visible = false;
			stage.focus = inputTextArea.textField;
			evt.stopImmediatePropagation();
		}
		private function onMouseOverFacePanel(evt:MouseEvent):void
		{
			facePanel.visible = true;
		}
		private function onClickFacePanel(evt:MouseEvent):void
		{
			facePanel.visible = false;
			var number:String = ((curFaceX+1) + (curFaceY*8)).toString();
			if(number.length == 1)
				number = "0" + number;
			sendMessageExt("/:"+number.toString());
		}
		private function onMouseMoveFacePanel(evt:MouseEvent):void
		{
			var targetX:Number = (evt.target as MovieClip).mouseX;
			var targetY:Number = (evt.target as MovieClip).mouseY;
			if(!faceRect.contains(targetX, targetY) || targetY <= 0)
			{
				facePanel.rect.visible = false;
				return;
			}

			var obj:Object = getSelectPos(targetX, targetY);
			var point1:Point = obj.point1;
			var point2:Point = obj.point2;
			if(curFaceX != point1.x)
			{
				facePanel.rect.visible = true;
				curFaceX = point1.x;
				facePanel.rect.x = point2.x;
			}
			if(curFaceY != point1.y)
			{
				facePanel.rect.visible = true;
				curFaceY = point1.y;
				facePanel.rect.y = point2.y;
			}
		}
		private function getSelectPos(mouseX:Number, mouseY:Number):Object
		{
			var tileWidth:Number = faceRect.width / 8;
			var tileHeight:Number = faceRect.height / 5;
			var point1:Point = new Point();
			var point2:Point = new Point();
			point1.x = (Math.ceil(mouseX / tileWidth)-1);
			point1.y = (Math.ceil(mouseY / tileHeight)-1);
			point2.x = point1.x * tileWidth;
			point2.y = point1.y * tileHeight;
			return {point1:point1, point2:point2};
		}
		public function init():void
		{
			facePanel.visible = false;
			normalList.visible = false;
			inputTextArea.clear();
		}
		
		public function show(visible:Boolean):void
		{
			if(visible)
				ChessLobby.chat.addChild(this);
			else if(parent)
				parent.removeChild(this);
		}
		
		static public function get i():ChatPanel
		{
			return Singleton.getInstanceOrCreate(ChatPanel) as ChatPanel;
		}
	}
}